#pragma once
#include <string>
#include <vector>
#include <array>
#include <yaml-cpp/yaml.h>

namespace config {

// 软件配置结构体
struct SoftwareConfig {
    struct {
        std::string version{"1.0.0"};
        std::string name{"DL Inference System"};
        std::string env{"development"};
    } basic;

    struct {
        int thread_pool_size{4};
        int gpu_id{0};
        bool cuda_enabled{true};
        int memory_limit{4096};  // MB
    } system;

    struct {
        std::string level{"debug"};
        std::string path{"../logs/"};
        size_t max_size{10485760};  // 10MB
        int max_files{10};
        int flush_interval{3};      // seconds
        bool console_output{true};
        bool file_output{true};
    } log;

    struct {
        std::string save_path{"../data/"};
        bool auto_clean{true};
        size_t max_space{10240};    // MB
        int clean_threshold{90};     // %
    } storage;
};

// 相机配置结构体
struct CameraConfig {
    std::string type{"usb"};        // usb/gige/other
    int device_id{0};
    std::string serial_number;
    
    struct {
        int width{1920};
        int height{1080};
    } resolution;
    
    int frame_rate{30};
    
    struct {
        std::string mode{"auto"};   // auto/manual
        int value{-6};
    } exposure;
    
    struct {
        std::string mode{"auto"};   // auto/manual
        int value{10};
    } gain;
    
    struct {
        std::string mode{"auto"};   // auto/manual
        float r_gain{1.5f};
        float g_gain{1.0f};
        float b_gain{1.2f};
    } white_balance;
    
    std::string format{"MJPG"};     // MJPG/YUYV/BGR
    int buffer_size{3};
};

// 算法配置结构体
struct AlgorithmConfig {
    struct {
        std::string name{"yolov5s"};
        std::string type{"detection"};
        std::string path{"../models/yolov5s.engine"};
        std::string device{"gpu"};
        int estimate_time{20};       // ms
        std::array<int, 2> input_size{640, 640};
        int batch_size{1};
        std::string precision{"FP16"};
        std::array<int, 2> output_size{1920, 1080};
        bool need_preprocess{true};
        bool need_postprocess{true};

        struct {
            std::array<int, 4> shape{1, 3, 640, 640};  // NCHW
            std::string dtype{"float32"};
            std::array<float, 2> range{0.0f, 1.0f};
        } input;

        struct {
            std::vector<std::string> names{"output0", "output1"};
            std::vector<std::vector<int>> shapes{{1, 25200, 85}, {1, 25200, 4}};
        } output;
    } model;

    struct {
        std::string type{"detection_preprocess"};
        struct {
            std::string color_space{"BGR"};
            std::array<int, 2> size{1080, 1920};
            int depth{3};
            int batch_size{1};
        } input;
        struct {
            std::array<int, 2> size{640, 640};
            int depth{3};
            int batch_size{1};
        } output;
    } preprocess;

    struct {
        std::string type{"detection_postprocess"};
        struct {
            std::array<int, 3> shape{1, 25200, 85};
            int depth{85};
            int batch_size{1};
        } input;
        struct {
            std::array<int, 3> shape{1, 100, 6};
            int depth{6};
            int batch_size{1};
        } output;
    } postprocess;
};

// 网络配置结构体
struct NetworkConfig {
    struct {
        struct {
            bool enabled{true};
            std::string ip{"0.0.0.0"};
            int port{8080};
            int max_connections{5};
            int timeout{3000};       // ms
            int buffer_size{4096};   // bytes
            bool keep_alive{true};
        } server;

        struct {
            std::string name{"client1"};
            bool enabled{true};
            std::string ip{"192.168.1.100"};
            int port{8081};
            bool auto_reconnect{true};
            int reconnect_interval{1000};  // ms
        } client;
    } tcp;

    struct {
        bool enabled{true};
        std::string local_ip{"0.0.0.0"};
        int local_port{8082};
        std::string remote_ip{"192.168.1.200"};
        int remote_port{8083};
        int buffer_size{65536};
        struct {
            bool enabled{false};
            std::string group{"224.0.0.1"};
            int port{8084};
        } multicast;
    } udp;

    struct {
        std::string type{"json"};    // json/binary/custom
        std::string version{"1.0"};
        int heartbeat_interval{1000}; // ms
        int max_packet_size{65536};
        bool compression{false};
    } protocol;
};

// 显示配置结构体
struct DisplayConfig {
    struct {
        std::string title{"DL Inference System"};
        int width{1280};
        int height{720};
        bool fullscreen{false};
        int fps{30};
        bool vsync{true};
    } window;

    struct {
        bool show_fps{true};
        bool show_detection{true};
        bool show_tracking{true};
        bool show_statistics{true};
        bool debug_view{false};
    } content;

    struct {
        struct {
            std::string name{"Arial"};
            int size{12};
            int thickness{1};
            std::array<int, 3> color{255, 255, 255};  // BGR
        } font;

        struct {
            int thickness{2};
            std::array<int, 2> text_offset{0, -10};
            struct {
                std::array<int, 3> person{0, 255, 0};   // BGR
                std::array<int, 3> car{255, 0, 0};
                std::array<int, 3> truck{0, 0, 255};
            } colors;
        } box;

        struct {
            bool enabled{true};
            int max_length{30};
            int thickness{2};
            bool fade{true};
        } trajectory;
    } drawing;

    struct {
        struct {
            int x{0};
            int y{0};
            int width{960};
            int height{720};
        } main_view;

        struct {
            int x{960};
            int y{0};
            int width{320};
            int height{720};
            std::array<int, 3> background_color{45, 45, 45};
        } info_panel;
    } layout;
};

} // namespace config
